package com.xxx.kjds.controller;

import com.xxx.kjds.entity.DataDictionary;
import com.xxx.kjds.entity.DataDictionaryExample;
import com.xxx.kjds.service.DictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/dictionary")
public class DictionaryController {

    @Autowired
    private DictionaryService dictionaryService;

    /*
     * 数据字典新增，管理员
     * */
    @PostMapping(value = "/add")
    @ResponseBody
    public String insert(HttpServletRequest request) {
        String type = request.getParameter("type");
        String description = request.getParameter("description");
        String code = request.getParameter("code");
        String value = request.getParameter("value");

        if (type.equals("") || description.equals("") || code.equals("") || value.equals("")) {
            return "四个字段不允许为空，请重新输入";
        }
        DataDictionary dataDictionary = new DataDictionary();
        dataDictionary.setDictType(type);
        dataDictionary.setDictDescription(description);
        dataDictionary.setCode(code);
        dataDictionary.setCodeValue(value);

        int result = dictionaryService.insert(dataDictionary);
        if (result > 0)
            return "插入成功";
        return "插入失败";
    }

    /*
     * 数据字典删除，管理员
     * */
    @GetMapping(value = "/delete")
    @ResponseBody
    public String deleteById(HttpServletRequest request) {
        Integer id = Integer.valueOf(request.getParameter("id"));
        int result = dictionaryService.deleteByPrimaryKey(id);
        if (result > 0)
            return "删除成功";
        return "删除失败";
    }

    /*
     * 数据字典修改，管理员
     * */
    @PostMapping(value = "/update")
    @ResponseBody
    public String updateById(HttpServletRequest request) {
        //获取id
        Integer id = Integer.valueOf(request.getParameter("id"));
        DataDictionary dataDictionary = new DataDictionary();
        dataDictionary = dictionaryService.selectByPrimaryKey(id);

        //获取除id外值
        String type = request.getParameter("type");
        String description = request.getParameter("description");
        String code = request.getParameter("code");
        String value = request.getParameter("value");

        //检查除id外值，均不为空
        if (type.equals("") || description.equals("") || code.equals("") || value.equals("")) {
            return "所有字段不允许为空值，请重新输入！";
        }

        //更新
        dataDictionary.setDictType(type);
        dataDictionary.setDictDescription(description);
        dataDictionary.setCode(code);
        dataDictionary.setCodeValue(value);
        int result = dictionaryService.updateByPrimaryKey(dataDictionary);
        if (result > 0)
            return "修改成功";
        return "修改失败";
    }

    /*@GetMapping(value = "/selectById{id}")
    public dataDictionary selectById(@RequestParam("id") Integer id) {
        dataDictionary dataDictionary = dictionaryService.selectByPrimaryKey(id);
        return dataDictionary;
    }*/

    /*
     * 数据字典列表条件查询，管理员
     * */
    @PostMapping(value = "/selectByType")
    @ResponseBody
    public List<DataDictionary> selectByType(HttpServletRequest request) {
        String type = request.getParameter("type");
        DataDictionaryExample dataDictionaryExample = new DataDictionaryExample();
        DataDictionaryExample.Criteria criteria = dataDictionaryExample.createCriteria();
        criteria.andDictTypeEqualTo(type);
        return dictionaryService.selectByExample(dataDictionaryExample);
    }

    //获取数据字典表列表
    @RequestMapping(value = "/getDicList", method = RequestMethod.POST)
    @ResponseBody
    public List<DataDictionary> getDicList(HttpServletResponse response) {
        return dictionaryService.getList();
    }
}
